'''
Created on Jun 29, 2010

@author: christianp
'''
import wx
from BruTile.Pointf import Pointf
from MapTransformHelpers import MapTransformHelpers
from BruTile.Extent import Extent
from BruTile.Utilities import Utilities
import threading

class MouseHandler(object):

    __map_control = None
    __left_mouse_pos = Pointf(0,0)

    __seedMinPos = None
    __seedMaxPos = None
    
    def __init__(self, map_control):
        self.__map_control = map_control
    
    def onScrollwheel(self, event):
        
        for t in threading.enumerate():
            print("Thread: "+t.getName())
        
        scroll_direction = event.GetWheelRotation()
        x, y = event.GetPosition()
       
        if (x < 0):
            x = 0
        if (y < 0):
            y = 0
        
        if (scroll_direction > 0):
            self.__map_control.zoomIn(Pointf(x,y))
        else:
            self.__map_control.zoomOut(Pointf(x,y))
    
    def setSeedExtent(self):
        if not self.__seedMinPos is None and not self.__seedMaxPos is None:
            extent = Utilities.getExtent(self.__seedMinPos.x, self.__seedMinPos.y, self.__seedMaxPos.x, self.__seedMaxPos.y)
            self.__map_control.updateProperties(extent)
    
    def rightMouseUp(self, event):
        self.__seedMinPos = None
        self.__seedMaxPos = None
    
    def rightMouseDown(self, event):
        if not(event.LeftIsDown()):
            x,y = event.GetPosition()
            self.__seedMinPos = self.__map_control.map_transform.mapToWorld(x, y)
            self.setSeedExtent()
    
    def leftMouseDown(self, event):
        x,y = event.GetPosition()
        self.__left_mouse_pos = Pointf(x,y)
        
    def mouseMove(self, event):
        x,y = event.GetPosition()
        new_mouse_pos = Pointf(x,y)
        if (event.LeftIsDown()):
            MapTransformHelpers.pan(self.__map_control.map_transform, new_mouse_pos, self.__left_mouse_pos)
            self.__left_mouse_pos = new_mouse_pos
            self.__map_control.refresh()
        elif(event.RightIsDown()):
            self.__seedMaxPos = self.__map_control.map_transform.mapToWorld(x, y)
            self.setSeedExtent()
